package com.store.app.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.hyphenate.EMCallBack;
import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.database.Database;
import com.store.app.database.DatabaseMethod;
import com.store.app.hyphenate.HyphenateHelper;
import com.store.app.net.NetRequest;
import com.store.app.push.PushUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2016/7/7.
 */
public class GoToLogin {
    private Context context;
     public  GoToLogin(Context context){
          this.context=context;
      }
    public void setGoToLogin(){
        try {
            Database database = new Database(context);
            DatabaseMethod method = new DatabaseMethod(database);
            method.clearFeedTable();
            SharedPreferences preferences;
            preferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(MainActivity.PREF_APP_TOKEN, "").commit();
            editor.putString(MainActivity.PREF_SECURITY_CODE, "").commit();
            editor.putString(MainActivity.PREF_USER_TOKEN, "").commit();
            editor.putString(MainActivity.PREF_USER_SECURITY_CODE, "").commit();
            editor.putString(MainActivity.PREF_MEMBER_ID, "").commit();
            editor.putString("member_id", "").commit();
            editor.putString("area_id", "").commit();
            editor.putString("stores_name", "").commit();
            editor.putString("store_desc", "").commit();
            editor.putString("contact_person", "").commit();
            editor.putString("stores_type_key", "").commit();
            editor.putString("business_type_key", "").commit();
            editor.putString("contact_tel", "").commit();
            editor.putString("address", "").commit();
            editor.putString("neighbor_pic_path", "").commit();
            editor.putString("longitude", "").commit();
            editor.putString("latitude", "").commit();
            Log.v("zyl", "跳转到首页1");
            //EMClient.getInstance().logout(true);
            //HyphenateHelper.getInstance().logout(false,null);
            Intent intent = new Intent();
            intent.putExtra("log_again", "true");
            intent.setClass(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            ExitApplication.getInstance().exit();
        }catch (Exception e){
            e.printStackTrace();
            Log.v("zyl","跳到登陆页面失败");
        }
    }

    public boolean setGoToLogin(JSONObject obj){
        try {

            String errorMsg = obj.optString("error_msg");
            if(errorMsg.contains("token")){


            Database database = new Database(context);
            DatabaseMethod method = new DatabaseMethod(database);
            method.clearFeedTable();
            SharedPreferences preferences;
            preferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(MainActivity.PREF_APP_TOKEN, "").commit();
            editor.putString(MainActivity.PREF_SECURITY_CODE, "").commit();
            editor.putString(MainActivity.PREF_USER_TOKEN, "").commit();
            editor.putString(MainActivity.PREF_USER_SECURITY_CODE, "").commit();
            editor.putString(MainActivity.PREF_MEMBER_ID, "").commit();
            editor.putString("member_id", "").commit();
            editor.putString("area_id", "").commit();
            editor.putString("stores_name", "").commit();
            editor.putString("store_desc", "").commit();
            editor.putString("contact_person", "").commit();
            editor.putString("stores_type_key", "").commit();
            editor.putString("business_type_key", "").commit();
            editor.putString("contact_tel", "").commit();
            editor.putString("address", "").commit();
            editor.putString("neighbor_pic_path", "").commit();
            editor.putString("longitude", "").commit();
            editor.putString("latitude", "").commit();
            Log.v("zyl", "跳转到首页2");
               // HyphenateHelper.getInstance().logout(false,null);
            Intent intent = new Intent();
                intent.putExtra("log_again", "true");
                intent.setClass(context, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            //ExitApplication.getInstance().exit();
              return true;
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.v("zyl","跳到登陆页面失败");
            return false;
        }
    }

    public void clearLoginInfo(Context context){
        logout();
        PushUtils.setAlias(context, "", null);
        LocationApplication.getData().clear();
        Database database = new Database(context);
        DatabaseMethod method = new DatabaseMethod(database);
        method.clearFeedTable();
        NetRequest netRequest = new NetRequest(context);
        netRequest.requestLogout(1);
        SharedPreferences preferences;
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(MainActivity.PREF_APP_TOKEN, "").commit();
        editor.putString(MainActivity.PREF_SECURITY_CODE, "").commit();
        editor.putString(MainActivity.PREF_USER_TOKEN, "").commit();
        editor.putString(MainActivity.PREF_USER_SECURITY_CODE, "").commit();
        editor.putString(MainActivity.PREF_MEMBER_ID, "").commit();

        editor.putString("member_id", "").commit();
        editor.putString("area_id", "").commit();
        editor.putString("stores_name", "").commit();
        editor.putString("store_desc", "").commit();
        editor.putString("contact_person", "").commit();
        editor.putString("stores_type_key", "").commit();
        editor.putString("business_type_key", "").commit();
        editor.putString("contact_tel", "").commit();
        editor.putString("address", "").commit();
        editor.putString("neighbor_pic_path", "").commit();
        editor.putString("longitude", "").commit();
        editor.putString("latitude", "").commit();
        editor.putString("im_user_id", "").commit();
    }
    public void logout() {
        HyphenateHelper.getInstance().logout(false, new EMCallBack() {

            @Override
            public void onSuccess() {
                Log.v("zyl", "退出环信成功");
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.v("zyl", "退出环信失败：" + message);
            }
        });
    }
}
